﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var SBatchNO = $.trim($("#txtSBatchNO").attr("value"));
    var SGOODSCODE = $.trim($("#txtSGOODSCODE").attr("value"));
    var SFlag = $('#SFlag').combobox('getValue');
    var CheckTimeS = $('#CheckTimeS').datebox('getValue');
    var CheckTimeE = $('#CheckTimeE').datebox('getValue');
    var CellNo = $('#CellNo').combobox('getValue');
    var SGOODSNAME = $.trim($("#txtSGOODSNAME").attr("value"));
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/Shelves/QueryOffShelves',
        title: '下架指令列表',
        queryParams: {
            SBatchNO: SBatchNO,
            SGOODSCODE: SGOODSCODE,
            SFlag: SFlag,
            CheckTimeS: CheckTimeS,
            CheckTimeE: CheckTimeE,
            CellNo: CellNo,
            SGOODSNAME: SGOODSNAME
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'OFFSHELVESID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'OFFSHELVESID', title: '下架指令ID', hidden: true },
                    { field: 'BATCHNO', title: '作业批次号', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', align: 'center', sortable: true },
                    { field: 'LOCATIONCODE', title: '货位', align: 'center', sortable: true },
                    { field: 'QTY', title: '数量', align: 'center', sortable: true },
                    { field: 'QTYEXCE', title: '执行数量', align: 'center', sortable: true },
                    { field: 'FLAG', title: '状态', width: 150, align: 'center', sortable: true, formatter: formatflag },
                    { field: 'OFFSHELVESDATE', title: '指定配送日期', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'CREATER', title: '创建人', align: 'center', sortable: true },
                    { field: 'CREATEDATE', title: '创建时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'EDITOR', title: '修改人', align: 'center', sortable: true },
                    { field: 'EDITDATE', title: '修改时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'REMARK', title: '备注', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

function formatflag(val) {
    if (val != null) {
        if (val == 0) {
            return "未处理";
        }
        if (val == 40) {
            return "在执行";
        }
        if (val == 100) {
            return "完结";
        }
    }
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}


//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增下架指令信息');
    $("#hiddenOFFSHELVESID").val("");
    $("#BatchNo").val("");
    $("#txtGoodsCode").val("");
    $("#txtQTY").val("");
    //var curr_time = new Date();
    //var date = curr_time.getFullYear() + "-";
    //date += curr_time.getMonth() + 1 + "-";
    //date += curr_time.getDate() + "-";
    //date += " 00:00:00";
    //$("#OffShelvesDate").datetimebox("setValue", date);
    //$("#txtQTYEXCE").val("");
    $("#OffShelvesDate").datebox("setValue", "");
    $("#txtRemark").val("");
    //$('#Flag').combobox("setValue", "0");
    url = '/Shelves/Saves';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改下架指令信息');
    $("#hiddenOFFSHELVESID").val(rows[0].OFFSHELVESID);
    $("#BatchNo").val(rows[0].BATCHNO);
    $("#txtGoodsCode").val(rows[0].GOODSCODE);
    $("#txtQTY").val(rows[0].QTY);
    var date = ChangeDateFormat(rows[0].OFFSHELVESDATE);
    $("#OffShelvesDate").datebox("setValue", date);
    //$("#txtQTYEXCE").val(rows[0].QTYEXCE);
    $("#txtRemark").val(rows[0].REMARK);
    //$('#Flag').combobox("setValue", rows[0].FLAG);
    url = '/Shelves/Saves';

}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].OFFSHELVESID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#location').form('submit', {
                url: '/Shelves/Delete' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function save() {
    var OFFSHELVESID = $.trim($("#hiddenOFFSHELVESID").attr("value"));
    var BatchNo = $.trim($("#BatchNo").attr("value"));
    var GoodsCode = $.trim($("#txtGoodsCode").attr("value"));
    var QTY = $.trim($("#txtQTY").attr("value"));
    var OffShelvesDate = $("#OffShelvesDate").datetimebox('getValue');
    //var Flag = $('#Flag').combobox('getValue');

    if (BatchNo == "") {
        $.messager.alert('提示', '作业批次号不能为空', 'warning');
        $("#BatchNo").focus();
        return false;
    }
    if (GoodsCode == "") {
        $.messager.alert('提示', '商品不能为空', 'warning');
        $("#txtGoodsCode").focus();
        return false;
    }
    if (QTY == "") {
        $.messager.alert('提示', '数量不能为空', 'warning');
        $("#txtQTY").focus();
        return false;
    }
    if (OffShelvesDate == "") {
        $.messager.alert('提示', '指定配送日期不能为空', 'warning');
        $("#OffShelvesDate").focus();
        return false;
    }
    //if (Flag == "") {
    //    $.messager.alert('提示', '状态不能为空', 'warning');
    //    $("#Flag").focus();
    //    return false;
    //}


    $('#location').form('submit', {
        url: url + '?OFFSHELVESID=' + OFFSHELVESID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

